<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title></title>
  </head>
  <style>
    *{padding: 0;margin: 0;}
    .box{
      width: 200px;
      height: 200px;
      background: #ccc;
      transform: translate(0,0);
    }
  </style>
  <body>
    <div class="box"></div>
    <script>
      let boxDom = document.querySelectorAll(".box")[0];
      console.log(boxDom)
      let x=0,y=0;//实际是top和left
      boxDom.addEventListener("touchstart",function(e){
        let tox = e.targetTouches[0].pageX - x;
        let toy = e.targetTouches[0].pageY - y;
        let xs = e.targetTouches[0].pageX;
        let ys = e.targetTouches[0].pageY;
        let dir = '';
        function move(e){
          if(!dir){
            if(Math.abs(xs-e.targetTouches[0].pageX)>=5){
              dir = 'x'
            }else if(Math.abs(ys-e.targetTouches[0].pageY)>=5){
              dir = 'y'
            }
          }else{
            if(dir == 'x'){
              x = e.targetTouches[0].pageX - tox;
            }else if(dir == 'y'){
              y = e.targetTouches[0].pageY - toy;
              console.log(y)
            }
          }
          boxDom.style.transform= `translate(${x}px,${y}px)`;
        }
        function end(e){
          boxDom.removeEventListener("touchmove",move,false) 
          boxDom.removeEventListener("touchend",end,false) 
        }
        boxDom.addEventListener("touchmove",move,false);
        boxDom.addEventListener("touchend",end,false);
      },false)
    </script>
  </body>
</html>
